"""初始数据库迁移

Revision ID: 20240320_initial
Revises: 
Create Date: 2024-03-20 15:30:00.000000

"""
from alembic import op
import sqlalchemy as sa
from sqlalchemy.dialects import sqlite

# revision identifiers
revision = '20240320_initial'
down_revision = None
branch_labels = None
depends_on = None

def upgrade() -> None:
    # 创建用户配置表
    op.create_table(
        'user_configs',
        sa.Column('id', sa.Integer(), nullable=False),
        sa.Column('api_key', sa.String(), nullable=True),
        sa.Column('theme', sa.String(), server_default='light', nullable=False),
        sa.Column('language', sa.String(), server_default='zh', nullable=False),
        sa.Column('max_tokens', sa.Integer(), server_default='1000', nullable=False),
        sa.Column('temperature', sa.Float(), server_default='0.7', nullable=False),
        sa.Column('created_at', sa.DateTime(timezone=True), server_default=sa.text('CURRENT_TIMESTAMP'), nullable=False),
        sa.Column('updated_at', sa.DateTime(timezone=True), server_default=sa.text('CURRENT_TIMESTAMP'), nullable=False),
        sa.PrimaryKeyConstraint('id')
    )
    
    # 创建历史记录表
    op.create_table(
        'histories',
        sa.Column('id', sa.Integer(), nullable=False),
        sa.Column('type', sa.String(), nullable=False),
        sa.Column('input_text', sa.Text(), nullable=False),
        sa.Column('output_text', sa.Text(), nullable=False),
        sa.Column('parameters', sqlite.JSON(), nullable=True),
        sa.Column('created_at', sa.DateTime(timezone=True), server_default=sa.text('CURRENT_TIMESTAMP'), nullable=False),
        sa.PrimaryKeyConstraint('id')
    )
    
    # 创建索引
    op.create_index(op.f('ix_user_configs_id'), 'user_configs', ['id'], unique=False)
    op.create_index(op.f('ix_histories_id'), 'histories', ['id'], unique=False)
    op.create_index(op.f('ix_histories_type'), 'histories', ['type'], unique=False)

def downgrade() -> None:
    # 删除索引
    op.drop_index(op.f('ix_histories_type'), table_name='histories')
    op.drop_index(op.f('ix_histories_id'), table_name='histories')
    op.drop_index(op.f('ix_user_configs_id'), table_name='user_configs')
    
    # 删除表
    op.drop_table('histories')
    op.drop_table('user_configs') 